// 引入模块
import { Sequelize } from 'sequelize'
import { app } from 'electron'
import { Configrate, DBConfig } from './config'
import { AssetInit } from '../model/asset'
import { BorrowingInit } from '../model/borrowing'
import { IncomeInit } from '../model/income'
import { InvestmentInit } from '../model/investment'
import { LiabilitiesInit } from '../model/liabilities'
import { NameTextInit } from '../model/name_text'
import { UserInit } from '../model/user'
export class DBConfigrate {
  public static sequelizeInstance: Sequelize
  static init(): void {
    // 读取配置
    const config: DBConfig = Configrate.getConfig()
    // 根据配置实例化seq
    this.sequelizeInstance = new Sequelize(config.dbname, config.uname, config.upwd, {
      storage: config.storage,
      dialect: 'sqlite',
      query: { raw: true },
      logging: !app.isPackaged,
      define: {
        underscored: config.define.underscored,
        freezeTableName: true,
        timestamps: config.define.timestamps
      }
    })
    AssetInit.init(this.sequelizeInstance)
    BorrowingInit.init(this.sequelizeInstance)
    IncomeInit.init(this.sequelizeInstance)
    InvestmentInit.init(this.sequelizeInstance)
    LiabilitiesInit.init(this.sequelizeInstance)
    NameTextInit.init(this.sequelizeInstance)
    UserInit.init(this.sequelizeInstance)
    // const sequelizeInstance = new Sequelize({
    //   database: config.dbname,
    //   dialect: 'sqlite',
    //   username: config.uname,
    //   password: config.upwd,
    //   storage: config.storage,
    //   models: [Asset, Borrowing, Income, Investment, Liabilities, NameText, User],
    //   query: { raw: true },
    //   define: {
    //     underscored: config.define.underscored,
    //     freezeTableName: true,
    //     timestamps: config.define.timestamps
    //   }
    // })
    this.sequelizeInstance.authenticate()
    this.sequelizeInstance.sync({
      logging: false,
      force: false // true:启用后会删除表再创建表
    })
  }
}
